package com.irdstudio.efp.esb.api.service.yed;

import com.alibaba.fastjson.JSON;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.constant.EsbRetCodeStandard;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.MessageRuleCheckUtil;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.basic.framework.redis.redisutil.RedisUtil;
import com.irdstudio.efp.esb.api.bean.wsd.credit.resp.NlsWsdFirstRespBean;
import com.irdstudio.efp.esb.api.bean.yed.req.NlsCreditOrApplOnWayReqBean;
import com.irdstudio.efp.esb.api.bean.yed.resp.NlsCreditOrApplOnWayRespBean;
import com.irdstudio.efp.esb.api.common.EsbRespServiceHandler;
import com.irdstudio.efp.esb.api.service.wsd.NlsWsdProcessServcie;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import com.irdstudio.efp.nls.service.vo.sx.NlsCreditInfoVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.irdstudio.basic.framework.redis.redisutil.keys.Ocm_Nls_Key.ESBRESPSERVICEBEAN_KEY;

@Service
public class NlsYedProcessService implements FrameworkService {

    // 日志信息
    private static final Logger logger = LoggerFactory.getLogger(NlsYedProcessService.class);

    // redis服务
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 需求编号:
     * 问题编号:复审验证信息
     * 开发人员: zhangxc
     * 创建日期:2020年7月28日11:24:23
     * 功能描述:
     * 修改日期:2020年7月28日11:24:28
     * 修改描述:
     */
    public EsbRespServiceBean processYedCreditApply(NlsCreditOrApplOnWayReqBean reqWsdCreditBean, EsbReqServiceBean esbReqServiceBean,String isflag) throws ESBException {
        try {
            // 校验必要的信息
            validateYedReqInfo(reqWsdCreditBean);
            // 插入返回信息
            NlsCreditOrApplOnWayRespBean successRespBean = getSuccessRespBean(isflag);
            // 清除否则影响后续流程
            TraceUtil.clear();
            EsbRespServiceBean esbRespServiceBean = EsbRespServiceHandler.esbSuccessHandler(esbReqServiceBean, successRespBean);
            setEsbRespServiceBean(reqWsdCreditBean.getRequestId() + "_" + reqWsdCreditBean.getApplySeq(), esbRespServiceBean);
            return esbRespServiceBean;
        } catch (ESBException e) {
            throw new ESBException(e.getErrorCode(), e.getMessage(), e);
        }
    }

    /**
     * 需求编号:
     * 问题编号:
     * 开发人员: zhangxc
     * 创建日期:2020年7月28日11:09:45
     * 功能描述:校验授信、用信信息
     * 修改日期:2020年7月28日11:09:50
     * 修改描述:
     */
    private void validateYedReqInfo(NlsCreditOrApplOnWayReqBean reqWsdCreditBean) throws ESBException {
        String pdNm = reqWsdCreditBean.getPrdCode();
        if (!"11110004".equals(pdNm)) { //产品编号需要修改
            logger.error("查询在途授信【REQUESTID】:+"+reqWsdCreditBean.getRequestId()+" 【APPLYNO】:"+reqWsdCreditBean.getApplySeq()+" 产品名称不正确!!!");
            throw new ESBException(EsbRetCodeStandard.CPXXJYBTG.getValue(), "产品编号不正确!!!");
        }
        if(StringUtils.isBlank(reqWsdCreditBean.getQryType()) && StringUtils.isBlank(reqWsdCreditBean.getCertType())){
            logger.error("查询在途授信【REQUESTID】:+"+reqWsdCreditBean.getRequestId()+" 【APPLYNO】:"+reqWsdCreditBean.getApplySeq()+" 查询类型、证件类型不能同时为空!!!");
            throw new ESBException(EsbRetCodeStandard.JKYDCSJYBTG.getValue(), "查询类型、证件类型不能同时为空!!!");
        }

        if(StringUtils.isNotBlank(reqWsdCreditBean.getCertCode())){
            try {
                // 身份证校验
                MessageRuleCheckUtil.validateIdNo18(reqWsdCreditBean.getCertCode(), EsbRetCodeStandard.CPXXJYBTG.getValue());
            } catch (BizException e) {
                logger.error("查询在途授信【REQUESTID】:+"+reqWsdCreditBean.getRequestId()+" 【APPLYNO】:"+reqWsdCreditBean.getApplySeq()+" 证件号码格式错误!!!");
                throw new ESBException(EsbRetCodeStandard.JKYDCSJYBTG.getValue(), "证件号码格式错误!!!");
            }
        }

    }

    /**
     * 需求编号:
     * 问题编号:
     * 开发人员: zhangxc
     * 创建日期:2020年7月28日11:20:04
     * 功能描述:获取成功交易返回Bean
     * 修改日期:2020年7月28日11:20:09
     * 修改描述:
     */
    public NlsCreditOrApplOnWayRespBean getSuccessRespBean(String isCreditApply) {
        NlsCreditOrApplOnWayRespBean nlsCreditOrApplOnWayRespBean = new NlsCreditOrApplOnWayRespBean();
        nlsCreditOrApplOnWayRespBean.setRetCd(EsbBizEnums.RetCodeEnum.SUCCESS.getVALUE());
        nlsCreditOrApplOnWayRespBean.setRetMsg("交易成功");
        nlsCreditOrApplOnWayRespBean.setIsCreditApply(isCreditApply);
        return nlsCreditOrApplOnWayRespBean;
    }

    public void setEsbRespServiceBean(String key, EsbRespServiceBean esbRespServiceBean) {
        redisUtil.set(ESBRESPSERVICEBEAN_KEY.getPrefix() + key, JSON.toJSONString(esbRespServiceBean));
    }

    /**
     * 需求编号:
     * 问题编号:
     * 开发人员: zhangxc
     * 创建日期:2020年7月28日11:20:04
     * 功能描述:获取成功交易返回Bean
     * 修改日期:2020年7月28日11:20:09
     * 修改描述:
     */
    public NlsCreditOrApplOnWayRespBean getFailureRespBean(String code, String msg) {
        NlsCreditOrApplOnWayRespBean nlsWsdFirstRespBean = new NlsCreditOrApplOnWayRespBean();
        nlsWsdFirstRespBean.setRetCd(code);
        nlsWsdFirstRespBean.setRetMsg(msg);
        nlsWsdFirstRespBean.setIsCreditApply("");
        return nlsWsdFirstRespBean;
    }
}
